import axios from 'axios';

// 创建axios实例
let axiosPms = {
    timeout: 15000, // 请求超时时间
    // headers:{
    //     'Content-Type': 'application/json;charset=UTF-8',
    // }
}


// 后期做完登陆加
if (localStorage.getItem("user_id")) {
    axiosPms.AccountId = localStorage.getItem("user_id");
}
//     个人收藏的接口
axiosPms.AccountId = localStorage.getItem("user_id");
/*     config.headers.AccountId = '21afb60a77cfffd866e95cc0ee79c535435d0bf77b54566c351c817eaee84b04a062b7adb55c73a66f5741051df3ab45'; */

let currentUrl = location.href;
if(currentUrl.indexOf('//m-dy.zsjx114.com') > -1){  //正式的
    axios.defaults.baseURL = location.protocol + "//api-dy-server.zsjx114.com/";
}else{  //测试的
    axios.defaults.baseURL = location.protocol + "//test-api-dy-server.zsjx114.com/";
}

const service = axios.create(axiosPms);

// request拦截器
service.interceptors.request.use(config => {
    // Do something before request is sent
    // if(config.method === 'post') {
    //     // let data = qs.parse(config.data);
    //     config.data = {
    //         ...config.data
    //     };
    // } else if(config.method === 'get') {
    //     config.params = {
    //         ...config.params
    //     }
    // }
    return config;
}, error => {
    Promise.reject(error);
});
// respone拦截器
service.interceptors.response.use(
    response => {
        const RES = response.data;
        if (response.status == '200' || response.status == '204' ) {
            return response.data;
        } else {
            // Toast({message: RES.message+'，状态码: '+RES.status,time:6000});
            return Promise.reject('error');
        }
    },
    error => {
        // console.log(error); // for debug
        // Toast({message: '接口响应错误：'+error,time:6000});
        return Promise.reject(error);
    }
);

export default service;